﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Common;
using SDLITS.Model;
using MySoft.Data;
namespace SDLITS.Web.Basic
{
    public partial class DepartmentChange : CommonBase
    {
        #region 页面初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUp();
                BindCharge();
                LoadData();
            }
        }
        protected void BindUp()
        {
            List<Department> lists = SDDbSession.Default.From<Department>().Where(Department ._ .DirID ==0).ToList();
            foreach (var item in lists)
            {
                drp_dir.Items.Add(item.DeptName, item.DeptID.ToString());
                int count = SDDbSession.Default.From<Department>().Where(Department._.DirID == item.DeptID).Count();
                if (count > 0)
                {
                    ResolveSubTree(item.DeptID, 0);
                }
            }
            drp_dir.Items.Insert(0, new ListItem("--顶级部门--", "0"));
        }
        private void ResolveSubTree(int id, int Leve)
        {
            Leve++;
            List<Department> items = SDDbSession.Default.From<Department>().Where(Department._.DirID == id).ToList();
            if (items.Count > 0)
            {
                foreach (Department row in items)
                {
                    int count = SDDbSession.Default.From<Department>().Where(Department._.DirID == row.DeptID).Count();
                    ListItem li = new ListItem();
                    li.SimulateTreeLevel = Leve;
                    li.Text = row.DeptName;
                    li.Value = row.DeptID.ToString();
                    drp_dir.Items.Add(li);
                    if (count > 0)
                    {
                        ResolveSubTree(row.DeptID, Leve);
                    }
                }
            }

        }
        protected void BindCharge()
        {
            WhereClip wc = SDLITS.Model.User._.CompanyID.IsNull();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                wc = wc && SDLITS.Model.User._.DeptID == Request["id"];
            }
            else if (!string.IsNullOrEmpty(Request["did"]))
            {
                wc = wc && SDLITS.Model.User._.DeptID == Request["did"];
            }
            List<SDLITS.Model.User> lists = SDDbSession.Default.From<SDLITS.Model.User>().Where(wc).ToList();
            drp_charge.DataSource = lists;
            drp_charge.DataTextField = "UserName";
            drp_charge.DataValueField = "UserID";
            drp_charge.DataBind();
        }
        #endregion

        #region 读取数据


        private void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                int id = int.Parse(Request["id"]);
                if (id > 0)
                {
                    Department Tm = SDDbSession.Default.From<Department>().Where(Department._.DeptID == id).ToFirst();
                    txt_Name.Text = Tm.DeptName;
                    txt_email.Text = Tm.Email;
                    txt_phone.Text = Tm.Phone;
                    txt_remark.Text = Tm.BackInfo;
                    txt_fax.Text = Tm.Fax;
                    drp_charge.SelectedValue = Tm.ChargeUserID.ToString();
                    drp_dir.SelectedValue = Tm.DirID.ToString();
                }
            }
            else if (!string.IsNullOrEmpty(Request["did"]))
            {
                int id = int.Parse(Request["did"]);
                if (id > 0)
                {
                    drp_dir.SelectedValue = Request["did"];
                    drp_dir.Enabled = false;
                }
            }
        }
        #endregion

        #region 保存数据
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(drp_charge.SelectedValue))
            {
                Alert.Show("您没有选择负责人！");
                return;
            }
            int returnvalue = 0;
            Department Tm = new Department();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Tm.DeptID = int.Parse(Request["id"]);
                Tm.Attach();
                Tm.DeptName = txt_Name.Text;
                Tm.ChargeUserID = int.Parse(drp_charge.SelectedValue);
                Tm.DirID = int.Parse(drp_dir.SelectedValue);
                Tm.Email = txt_email.Text;
                Tm.Phone = txt_phone.Text;
                Tm.BackInfo = txt_remark.Text;
                if (drp_dir.SelectedValue == "0")
                    Tm.UpJob = "已经是最高级了";
                else
                    Tm.UpJob = drp_dir.SelectedText;
                Tm.Fax = txt_fax.Text;
              //  Tm.TreeLever = getLever(); 
                returnvalue = SDDbSession.Default.Update<Department>(Tm);
            }
            else
            {
                Tm.DeAttach();
                Tm.DeptName = txt_Name.Text;
                Tm.ChargeUserID = int.Parse(drp_charge.SelectedValue);
                Tm.DirID = int.Parse(drp_dir.SelectedValue);
                Tm.Email = txt_email.Text;
                Tm.Phone = txt_phone.Text;
                Tm.BackInfo = txt_remark.Text;
                if (drp_dir.SelectedValue == "0")
                    Tm.UpJob = "已经是最高级了";
                else
                    Tm.UpJob = drp_dir.SelectedText;
                Tm.Fax = txt_fax.Text;
               // Tm.TreeLever = getLever();
                returnvalue = SDDbSession.Default.Insert<Department>(Tm);
            }
            if (returnvalue > 0)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
            }
        }
        protected int getLever()
        {
            int lever = 0;
            if (drp_dir.SelectedValue != "0")
            {
                Department dept = SDDbSession.Default.From<Department>().Where(Department._.DeptID == drp_dir.SelectedValue).ToFirst();
                if(dept !=null)
                {
                    lever = int.Parse(dept.TreeLever.ToString()) + 1;
                }
            }
            return lever;
        }
        #endregion
    }
}